// index.tsx
import {Table, message, Button} from 'antd'
import React, {useEffect, useState} from 'react'
import type {ColumnsType} from 'antd/es/table'
import styles from './index.less'
import {getPublicDownloadUrl} from '@/service/common/upload'
import {downloadByUrl} from '@/service/common/download'

interface DataType {
	key: string
	name: string
	address: string
}
type PropsType = {
	kid?: any
	scroll?: any
	dataSource: any[]

	bordered?: boolean
}

const columns: ColumnsType<DataType> = [
	{
		title: '审批结果',
		colSpan: 2,
		align: 'center',
		width: 200,
		dataIndex: 'name',
	},
	{
		title: '',
		dataIndex: 'value',
		colSpan: 0,
		render: (text: number, record: any) => {
			// eslint-disable-next-line no-eval
			const user = eval(`(${record?.message})`)
			// eslint-disable-next-line no-eval
			const msg = eval(`(${user?.message})`)
			return (
				<>
					{record?.type === '1' ? (
						<>
							<span className="auditResults">
								{msg.type === 'self'
									? '作废'
									: record?.auditResults}
							</span>
							<span className="userName">{msg?.userName}</span>
							<span className="endTime">{record?.endTime}</span>
						</>
					) : null}
					{record?.type === '2' && record.flag ? (
						<>
							{record?.files?.map((item: any) => (
								<>
									<span className="fileName">
										{item?.filename}
									</span>
									<Button
										type="link"
										size="small"
										className="downOrupload"
										onClick={() => {
											downloadByUrl(
												item?.giftNamespace,
												item.filename,
											)
										}}>
										下载
									</Button>
								</>
							))}
						</>
					) : null}
					{record?.type === '2' && !record.flag ? (
						<>
							<span className="fileName">暂无附件</span>
						</>
					) : null}
					{record?.type === '3' ? <div>{msg?.message}</div> : null}
				</>
			)
		},
	},
]

const useAsyncTable = (props: PropsType) => {
	const {dataSource} = props
	const [data, setData] = useState<any>([])
	const [loading, setLoading] = useState<boolean>(false)
	// 初始化-附件列表
	const fileLists = (fileList: any[], callback: any) => {
		const list: any = fileList
			?.map((item: {url: string}) => item?.url?.split(','))
			?.flat(Infinity)
			?.filter((s: any) => s && s.trim())
		setLoading(() => true)
		getPublicDownloadUrl(list)
			.then((res: any) => {
				if (res?.success) {
					setLoading(() => false)
					callback(res?.data)
				}
			})
			.catch((err) => {
				message.error(err)
			})
	}
	useEffect(() => {
		const l: any = dataSource?.filter(
			(item: any, index: number) =>
				index !== 0 &&
				(item.nodeType === 1 || item.nodeType === 3) &&
				item?.assignee !== '',
		)
		fileLists(l, (res: any) => {
			const source: any[] = l
				?.map((item: any) => {
					const i = item
					const lstr = item?.url?.split(',')
					i.files = res.filter((ls: {id: any}) =>
						lstr?.includes(ls.id.toString()),
					)
					return i
				})
				?.map(
					(item: {
						activityName: string
						assignee: string
						url: string
						files: any
						message: string
						nodeType: number
					}) => {
						if (item.nodeType === 3) {
							// eslint-disable-next-line no-eval
							const user = eval(`(${item?.message})`)
							// eslint-disable-next-line no-eval
							const msg = eval(`(${user?.message})`)
							return [
								{
									name:
										msg.type === 'self'
											? '申请人'
											: item?.activityName,
									type: '1',
									value: item?.assignee,
									...item,
								},
								{
									name: '附件',
									type: '2',
									flag: false,
									value: [],
									...item,
								},
								{
									name: '备注',
									type: '3',
									value: item?.message,
									...item,
								},
							]
						}
						return [
							{
								name: item?.activityName,
								type: '1',
								value: item?.assignee,
								...item,
							},
							{
								name: '附件',
								type: '2',
								flag: true,
								value: item?.files,
								...item,
							},
							{
								name: '备注',
								type: '3',
								value: item?.message,
								...item,
							},
						]
					},
				)
			const d = source?.reduce((acc, val) => acc.concat(val), [])
			setData(() => [...d])
		})
	}, [dataSource])
	return (
		<>
			{data?.length > 0 ? (
				<Table
					className={styles.histryTable}
					loading={loading}
					columns={columns}
					key={data}
					dataSource={data}
					bordered
					pagination={false}
				/>
			) : null}
		</>
	)
}
export default useAsyncTable
